
/**
  ******************************************************************************
  * @file    kea128_pit.c
  * @author  花溪村长
  * @version V1.0
  * @date    18-8-2015
  * @brief   This file contains all the functions prototypes for the GPIO 
  *          firmware library.
  ******************************************************************************
**/ 


#include "kea128_pit.h"

/**
  * @brief PIT init.
  * @param  PIT_InitStructure: 
	          PIT_InitStructure->CHANNELx Select the CHANNEL0 or the CHANNEL1 peripheral. 
						PIT_InitStructure->PIT_Mode Select PIT_Mode_ms or PIT_Mode_us modes.
						PIT_InitStructure->Timer  set need timer  
  *   This parameter can be one of the following values:
	* @param cn  Timer start value 
  * @retval None
  */
void PIT_Init(PIT_InitTypeDef *PIT_InitStructure)
{
	 uint32_t cn;
	 ASSERT(IS_PIT_CHANNEL(PIT_InitStructure->CHANNELx));
    //选择中断时间模式 ms和us
	 if(PIT_InitStructure->PIT_Mode == PIT_Mode_ms)
	 {
		 	cn = PIT_InitStructure->Timer * (SystemBusClock/1000);
	 }
	 else
	 {
		 cn = PIT_InitStructure->Timer * ((SystemBusClock/1000)/100);
	 }
		 
	 SIM_SCGC |= SIM_SCGC_PIT_MASK;     /*!< enable clock to PIT */
	 PIT_MCR &= ~PIT_MCR_MDIS_MASK;    //允许PIT标准时钟
	 PIT->CHANNEL[PIT_InitStructure->CHANNELx].LDVAL = cn;   //Timer开始值 向下计数
}
/**
  * @brief  Enables or disables the specified PIT.
  * @param  CHANNELx: Select the CHANNEL0 or the CHANNEL1 peripheral. 
  * @param  NewState: new state of the specified USARTx interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void PIT_Cmd(PITx_CHANNEL CHANNELx ,FunctionalState NewState)
{
	ASSERT(IS_PIT_CHANNEL(CHANNELx));
	
	if(NewState != DISABLE)
	{
		PIT->CHANNEL[CHANNELx].TCTRL |= PIT_TCTRL_TEN_MASK ;
	}
	else
	  PIT->CHANNEL[CHANNELx].TCTRL &= ~PIT_TCTRL_TEN_MASK ;
}
/**
  * @brief  Enables or disables the specified PIT Interrupt.
  * @param  CHANNELx: Select the CHANNEL0 or the CHANNEL1 peripheral. 
  * @param  NewState: new state of the specified USARTx interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void PIT_ITConfig(PITx_CHANNEL CHANNELx ,FunctionalState NewState)
{
	ASSERT(IS_PIT_CHANNEL(CHANNELx));
	if(NewState != DISABLE)
	{
		PIT->CHANNEL[CHANNELx].TCTRL |= PIT_TCTRL_TIE_MASK ;
	}
	else
	  PIT->CHANNEL[CHANNELx].TCTRL &= ~PIT_TCTRL_TIE_MASK ;
}
/**
  * @brief  Clear PIT Interrupt flag.
  * @param  CHANNELx: Select the CHANNEL0 or the CHANNEL1 peripheral. 
  * 
  *   
  * @retval None
  */
void PIT_ClearFlag(PITx_CHANNEL CHANNELx)
{
	ASSERT(IS_PIT_CHANNEL(CHANNELx));
	PIT->CHANNEL[CHANNELx].TFLG |= PIT_TFLG_TIF_MASK ;
}


